package cn.edu.cxcyzx.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.cxcyzx.conf.Role;
import cn.edu.cxcyzx.entity.Gitee;
import cn.edu.cxcyzx.entity.Members;
import cn.edu.cxcyzx.entity.Student;
import cn.edu.cxcyzx.entity.TaskProject;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.supervisor.ServiceFit;
import cn.edu.cxcyzx.util.MsgJSON;
import cn.edu.cxcyzx.util.PropertyPlaceholder;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Resource
	ServiceFit serviceFit;
	Role r=Role.STU;
	private Student stu=null;
	
	@RequestMapping("home")
	public ModelAndView HomePage(HttpSession session,@RequestParam(required=true,defaultValue="1")Integer pageNum){
		System.out.println("pageNum:"+pageNum);
		this.stu=(Student) session.getAttribute("student");
		if(stu==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		System.out.println("首页");
		ModelAndView modelAndView=new ModelAndView("student/stuIndex");
		modelAndView.addObject("taskNum", serviceFit.getTaskService().getTask_num(stu.getStuId()));
		PageHelper.startPage(pageNum,1);
		ResultDo<List<TaskProject>> resultDo=serviceFit.getTaskService().getTaskProject(stu.getStuId());
		if(resultDo.isSuccess()){
			System.out.println(resultDo.getResult().toString());
			PageInfo<TaskProject> pageInfo=new PageInfo<>(resultDo.getResult(),1);
			modelAndView.addObject("pageInfo",pageInfo);
		}else{
			modelAndView.addObject("errorMsg", "未查到任务信息！");
		}
		return modelAndView;
	}
	@RequestMapping("/update_taskProcess")
	@ResponseBody
	public MsgJSON update_taskProcess(String taskId,int projectId){
		System.out.println("跟新任务进度-projectId:"+taskId+",projectId:"+projectId);
		ResultDo<Integer> resultDo=serviceFit.getTaskService().update_process(taskId, projectId);
		if(resultDo.isSuccess()){
			return MsgJSON.success().add("process", resultDo.getResult());
		}else{
			return MsgJSON.fail().add("errorMsg", resultDo.getMessage());
		}
	}
	
	/**
	 * 个人中心
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("stuInfo")
	public ModelAndView stuInfoPage(HttpSession session,Model model){
		if(this.stu==null){
			System.out.println("请先登陆。。。。。。");
			return new ModelAndView("redirect:/login.jsp");
		}
		return new ModelAndView("student/stuInfo");
	}
	/**
	 * 修改个人信息
	 * @param student
	 * @return
	 */
	@RequestMapping("upstuInfo")
	public ModelAndView upstuInfo(Student student){
		System.out.println("..updateStu:"+student.toString());
		serviceFit.getStudentService().updateStu(student);
		return new ModelAndView("student/stuInfo");
	}
	/**
	 * 根据学号查询学生信息
	 * @param stuId
	 * @return
	 */
	@RequestMapping("/getStuById")
	@ResponseBody
	public MsgJSON getStu(@RequestParam(value="stuId")String stuId){
		
		ResultDo<Student> resultDo=serviceFit.getStudentService().getStu_byStuId(stuId);
		if(resultDo.isSuccess()){
			return MsgJSON.success().add("member_stu", resultDo.getResult());
		}else{
			return MsgJSON.fail();
		}
		
	}
	@RequestMapping("/insertMembers")
	@ResponseBody
	public MsgJSON insertMembers(String[] member_stuList,String groupId,String groupName,String gitee_proName,String gitee_full_name){
		System.out.println("gitee_proName:"+gitee_proName+",gitee_full_name:"+gitee_full_name);
		System.out.println(Arrays.toString(member_stuList));
		List<Members> list=new ArrayList<>();
		for (int i = 0; i < member_stuList.length; i++) {
			String stuName=serviceFit.getStudentService().getStuName(member_stuList[i]);
			list.add(new Members(member_stuList[i], stuName, groupId));
		}
		serviceFit.getMembersService().insertMembers(list,gitee_proName,gitee_full_name);
		int num=serviceFit.getMembersService().getMemberNum_groupId(groupId);
		serviceFit.getGroupService().set_groupName(groupName, groupId);
		return MsgJSON.success().add("msg", "提交成功,并添加"+num+"名学生.");
		
	}
	/**
	 * 获取班级名称,读取配置文件中信息
	 * @return
	 */
	@RequestMapping("/getClassNames")
	@ResponseBody
	public MsgJSON getStuClassName(){
		
		String[] names=PropertyPlaceholder.getProperties("className");
		if(names!=null&&names.length>0){
			System.out.println(Arrays.toString(names));
			return MsgJSON.success().add("classNames", names);
		}else{
			return MsgJSON.fail();
		}
	}
	/**
	 * 重新登陆
	 * @param session
	 * @return
	 */
	@RequestMapping("/relogin")
	@ResponseBody
	public MsgJSON reLogin(HttpSession session){
		session.removeAttribute("student");
		return MsgJSON.success();
	}
	/**
	 * 退出
	 * @param session
	 * @return
	 */
	@RequestMapping("quit")
	public String quit(HttpSession session){
		session.removeAttribute("student");
		return "redirect:/login.jsp";
	}
	
	
}
